<?php

namespace app\model\v1\recovery;

use crmeb\traits\ModelTrait;
use think\Db;
use think\Model;

class RecoveryQuestionCate extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 属性库管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getQuestionCateList($param = [])
    {
        $order = ["id"=>"desc"];

        $data["data"] = self::whereNull("delete_time")
            ->field("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text ")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("cate_name",'like', '%'.$param["word"].'%');
                if(!empty($param["cate_id"])) $query->where("type", $param["cate_id"]);
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::whereNull("delete_time")
            ->field("*")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("cate_name",'like', '%'.$param["word"].'%');
                if(!empty($param["cate_id"])) $query->where("type", $param["cate_id"]);
            })
            ->count();

        if(!empty($data['data'])){
            foreach ($data['data'] as $key => &$val){
                $val["type_text"] = self::getQuestionTypeText($val['type']);
            }
        }

        return $data;
    }


    public static function getQuestionTypeText($type)
    {
        $arr = [
            1 => '问题类型',
            2 => '举报类型',
            4 => '回收入驻类型',
            5 => '搬家入驻类型',
            6 => '发布店铺转让获取店铺类型',
            7 => '发布办公转租获取写字楼类型',
            8 => '发布办公转租获取写字楼装修',
            9 => '店铺标签',
            10 => '办公转租押金押方式',
            11 => '办公转租押金付方式',
            12 => '起/免租期',
            13 => '办公转租标签',
        ];
        return $arr[$type] ?? '';
    }


    /**
     * 获取类型配置列表
     * @return array[]
     */
    public static function getQuestionTypeList()
    {
        return [
            [
                "type_id" => 8,
                "type_name" => "写字楼装修",
            ],
            [
                "type_id" => 7,
                "type_name" => "写字楼类型",
            ],
            [
                "type_id" => 6,
                "type_name" => "商铺类型",
            ],
            [
                "type_id" => 5,
                "type_name" => "搬家入驻类型",
            ],
            [
                "type_id" => 4,
                "type_name" => "回收入驻类型",
            ],
            [
                "type_id" => 2,
                "type_name" => "用户举报类型",
            ],
        ];
    }


    /**
     * 获取属性库的配置类型列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addQuestionCate($param = [])
    {
        $add = [
            "uid"           => $param["admin_id"],  //创建人id
            "cate_name"     => $param["cate_name"] ?? '', //属性名称
            "type"          => $param["type"],  //类型
            "create_time"   => time(),
        ];
        return self::insert($add);
    }



    public static function updateQuestionCate($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["cate_name"]))     $update["cate_name"] = $param["cate_name"];  //分类名称
        if(!empty($param["type"]))          $update["type"] = $param["type"]; //类型

        return self::where("id", $param["id"])->update($update);
    }


    /**
     * 属性库管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableQuestionCateStatus($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }


    public static function getCateTextByIds($ids = '')
    {
        if(empty($ids)) return [];

        $ids = explode(",", $ids);
        $cate_name_data = self::whereIn("id", $ids)->column("cate_name") ?? [];
        if(empty($cate_name_data)) return [];
        return implode(",", $cate_name_data);
    }



}